import re
import sys

def count_text_statistics(file_path):
    """统计文本的字符数、单词数和句子数"""
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            text = file.read()
        
        # 基本统计
        chars = len(text)
        words = len(re.findall(r'\b[a-zA-Z]+\b', text))
        sentences = len(re.split(r'[.!?]+', text)) - 1
        
        return chars, words, sentences
        
    except FileNotFoundError:
        print(f"错误: 文件 {file_path} 不存在")
        sys.exit(1)
    except Exception as e:
        print(f"错误: {e}")
        sys.exit(1)

def main():
    """主函数 - 处理命令行参数并执行统计"""
    # 检查参数数量
    if len(sys.argv) < 2:
        print("用法: wc.exe [参数] [文件名]")
        print("参数:")
        print("  -c    统计字符数")
        print("  -w    统计单词数")
        print("  -s    统计句子数")
        print("  无参数 统计所有项目")
        sys.exit(1)
    
    # 解析参数
    show_chars = False
    show_words = False
    show_sentences = False
    filename = None
    
    # 遍历参数
    for arg in sys.argv[1:]:
        if arg == '-c':
            show_chars = True
        elif arg == '-w':
            show_words = True
        elif arg == '-s':
            show_sentences = True
        else:
            # 如果不是参数，则认为是文件名
            filename = arg
    
    # 如果没有指定任何统计项目，则显示全部
    if not (show_chars or show_words or show_sentences):
        show_chars = show_words = show_sentences = True
    
    # 检查文件名是否提供
    if filename is None:
        print("错误: 未指定文件名")
        sys.exit(1)
    
    # 执行统计
    chars, words, sentences = count_text_statistics(filename)
    
    # 显示结果
    results = []
    if show_chars:
        results.append(f"字符数: {chars}")
    if show_words:
        results.append(f"单词数: {words}")
    if show_sentences:
        results.append(f"句子数: {sentences}")
    
    print(", ".join(results))

if __name__ == "__main__":
    main()